// vars
var profileURL = './app/steamprofile.php?id=';
var loaderImageURL = './images/loading.gif'; 
var minFontSize = 8;

;(function($) {
    $.fn.textfill = function(options) {
        var fontSize = options.maxFontPixels;
        var ourText = $('span:visible:first', this);
        var maxHeight = 100;
        var maxWidth = 155;
        var textHeight;
        var textWidth;
        do {
            if (fontSize == minFontSize) {
                var str = ourText.text();
                var sub = 3; 
                do {
                    ourText.text(str.substring(0, (str.length - sub)) + '..');
                    textWidth = ourText.width();
                }
                while (textWidth > maxWidth);
                return this;       
            }
            ourText.css('font-size', fontSize);
            textHeight = ourText.height();
            textWidth = ourText.width();
            fontSize = fontSize - 1;
        } 
        while ((textHeight > maxHeight || textWidth > maxWidth) && fontSize >= minFontSize);
        return this;
    }
})(jQuery);

$(document).ready(function() {
    $('.donorprofile').each(function(i, obj) {
        this.innerHTML = '<img class="loadingImage" src="' + loaderImageURL + '" alt="loading" />Loading...';
        $(this).load(profileURL + this.title, function() {
            if ($.trim(this.innerHTML) == "") {
                $(this).remove();
                return;
            }
            this.title = $(this).find('.avatar')[0].title;
            $(this).find('.friendlyname').textfill({ maxFontPixels: 12 });
            $(this).find('.friendlyname').width('auto');
        });
    });
});